package com.mjkj.demo.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.mjkj.demo.constant.UserRoleConstants;
import com.mjkj.demo.core.result.Result;
import com.mjkj.demo.core.result.ResultGenerator;
import com.mjkj.demo.model.req.LoginReq;
import com.mjkj.demo.model.req.RegisterReq;
import com.mjkj.demo.model.vo.LoginVo;
import com.mjkj.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
/**
 * @author keeng
 * @since 2025/9/4
 */
@RestController
@RequestMapping("/user")
@Api(value = "登录", tags = "登录")
public class LoginController {

	@Resource
	private UserService userService;

	@SaIgnore
	@PostMapping("/login")
	@ApiOperation(value = "登录")
	public Result<LoginVo> login(@RequestBody @Valid LoginReq requestParams) {
		return ResultGenerator.genSuccessResult(userService.login(requestParams));
	}

	@SaIgnore
	@PostMapping("/register")
	@ApiOperation(value = "注册")
	public Result register(@RequestBody @Valid RegisterReq requestParams) {
		userService.register(requestParams);
		return ResultGenerator.genSuccessResult();
	}

	@SaCheckRole
	@PostMapping("/logout")
	@ApiOperation(value = "退出登录")
	public Result<String> logout() {
		StpUtil.logout();
		return ResultGenerator.genSuccessResult("退出成功");
	}

	@GetMapping("/isLogined")
	@ApiOperation(value = "是否登录")
    public Result<Boolean> isLogined() {
		return ResultGenerator.genSuccessResult(StpUtil.isLogin());
	}

	@SaCheckRole(UserRoleConstants.ADMIN_CODE)
	@GetMapping("/checkIsAdminRole")
	public Result<String> checkIsAdmin() {
		return ResultGenerator.genSuccessResult("有此权限");
	}

	@SaCheckRole(UserRoleConstants.REGULAR_USER_CODE)
	@GetMapping("/checkIsRegularUserRole")
    public Result<String> checkIsRegularUser() {
		return ResultGenerator.genSuccessResult("有此权限");
	}

	@SaCheckRole(UserRoleConstants.MEMBER_USER_CODE)
	@GetMapping("/checkIsMemberUserRole")
    public Result<String> checkIsMemberUser() {
		return ResultGenerator.genSuccessResult("有此权限");
	}
}
